
import React, { useEffect, useState } from 'react';
import {
    StatusBar,
} from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator , TransitionPresets } from '@react-navigation/stack';
import AsyncStorage from '@react-native-async-storage/async-storage';
import Guide from './src/views/Guide/Guide';
import Tabbar from './src/views/Tabbar/Tabbar'
import { registerServer } from './src/servers/app-servers';
import { useAtom } from 'jotai';
import { userInfoAtom } from './src/store/user';
import Storage from './src/utils/Storage'
import { SafeAreaView } from 'react-native-safe-area-context';
import TransitionLoading from './src/components/TransitionLoading'

const Stack = createStackNavigator()
function App(): React.JSX.Element {
    const [isFirstLaunch, setIsFirstLaunch] = useState<any>(null);

    const [userInfo , setUserInfo] = useAtom(userInfoAtom)

    const checkFirstLaunch = async () => {
        try {
            const guideFlag = await AsyncStorage.getItem('guideFlag');
            if (guideFlag === null) {
                // 如果 guideFlag 不存在，说明是第一次进入
                await AsyncStorage.setItem("guideFlag" , "true")
                setIsFirstLaunch(true);
            }else{
                setIsFirstLaunch(false);
            }
        } catch (error) {
            console.error('Failed to load guideFlag:', error);
        } 
    };

    const initApp = async () => {
        try {
            const res = await registerServer()
            Storage.setStorage("uuid" , res.data.user_info.uuid)
            setUserInfo(res.data.user_info)
            checkFirstLaunch();
        } catch (error) {
            console.log(error);
        }
    }
    useEffect(() => {
        initApp()
    }, []);
    if (isFirstLaunch === null) {
        // 可以在这里显示一个加载指示器
        return (
            <TransitionLoading />
        )
    }
    return (
        <SafeAreaView style={{ flex:1}} edges={['left', 'right', 'bottom']}>
            <StatusBar
                translucent 
                backgroundColor="transparent"
                barStyle={'dark-content'}
            />
            <NavigationContainer>
            <Stack.Navigator
                initialRouteName={isFirstLaunch ? 'Guide' : 'Tabbar'}
                screenOptions={{
                    cardStyle:{elevation:1},
                    ...TransitionPresets.SlideFromRightIOS,
                    headerShown: false,
                }}
            >
                <Stack.Screen
                    component={Guide}
                    name='Guide'
                />
                <Stack.Screen
                    component={Tabbar}
                    name='Tabbar'
                />
            </Stack.Navigator>
            </NavigationContainer>
        </SafeAreaView>
    );
}



export default App;
